'''
Created on 2014-5-19

@author: ASUS
'''
import json
import urllib
REQUEST_URL = 'http://112.124.1.3:8004'

class Category:
    def store_all_category(self):
        all_category_url = 'api/commodity'

    #use json.loads to convert str to json obj
    #also can use eval() or other funcs
        data = json.loads(urllib.urlopen('/'.join([REQUEST_URL,
                                               all_category_url])).read())
    
        file=open(r'../txt_package/all_categories.txt','a')
        for single in data:
            file.write(str(single))
            file.write('\n')
    
        file.close()
        
    def get_all_categories(self):
        f=open(r'../txt_package/all_categories.txt','r')
        l=[]
        for number in range(0,117):
            line=f.readline()
            info=eval(line)
            l.append(info)
        f.close()
        return l


    def get_categoryByASIN(self,asin):
        f=open(r'../txt_package/reviews_with_category_2.txt','r')
        while True:
            line=f.readline()
            if line:
                if line['ASIN']==asin:
                    return line['category']
                else:
                    continue
            else:
                break
    